package com.study.project.service.impl;

import com.study.project.entity.Rules;
import com.study.project.mapper.RulesMapper;
import com.study.project.service.RulesService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 告警规则表(Rules)表服务实现类
 *
 * @author makejava
 * @since 2024-07-05 17:47:57
 */
@Service("rulesService")
public class RulesServiceImpl implements RulesService {
    @Resource
    private RulesMapper rulesMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param ruleId 主键
     * @return 实例对象
     */
    @Override
    public Rules queryById(Integer ruleId) {
        return this.rulesMapper.queryById(ruleId);
    }

    /**
     * 分页查询
     *
     * @param rules       筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<Rules> queryByPage(Rules rules, PageRequest pageRequest) {
        long total = this.rulesMapper.count(rules);
        return new PageImpl<>(this.rulesMapper.queryAllByLimit(rules, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param rules 实例对象
     * @return 实例对象
     */
    @Override
    public Rules insert(Rules rules) {
        this.rulesMapper.insert(rules);
        return rules;
    }

    /**
     * 修改数据
     *
     * @param rules 实例对象
     * @return 实例对象
     */
    @Override
    public Rules update(Rules rules) {
        this.rulesMapper.update(rules, null);
        return this.queryById(rules.getRuleId());
    }

    /**
     * 通过主键删除数据
     *
     * @param ruleId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer ruleId) {
        return this.rulesMapper.deleteById(ruleId) > 0;
    }
}
